package esb.chapter5.validation.servicemix;

import javax.annotation.Resource;
import javax.jbi.messaging.DeliveryChannel;
import javax.jbi.messaging.ExchangeStatus;
import javax.jbi.messaging.Fault;
import javax.jbi.messaging.InOnly;
import javax.jbi.messaging.InOut;
import javax.jbi.messaging.MessageExchange;
import javax.jbi.messaging.MessagingException;
import javax.jbi.messaging.NormalizedMessage;
import javax.naming.InitialContext;
import javax.xml.transform.Source;

import org.apache.servicemix.MessageExchangeListener;
import org.apache.servicemix.client.ClientFactory;
import org.apache.servicemix.client.Destination;
import org.apache.servicemix.client.ServiceMixClient;

public class ErrorHandlerComponent implements MessageExchangeListener{
	
	@Resource
	private DeliveryChannel channel;
	
	public void onMessageExchange(MessageExchange exchange) throws MessagingException {
		System.out.println("ErrorHandlerComponent exchange !!!!!!!!!!! " + exchange.getStatus());
    	if (exchange.getStatus() == ExchangeStatus.ACTIVE) {
    		ServiceMixClient client = createClient();
    		System.out.println("ErrorHandlerComponent: STATUS IS ACTIVE!!!!!!!!!!!");
    		Destination destination = client.createDestination(
				"service:http://esbinaction.com/order/validation");
    		InOut validationExchange = destination.createInOutExchange();
    		NormalizedMessage message = validationExchange.getInMessage();
    		message.setContent(exchange.getMessage("in").getContent());
    		System.out.println("Sending message....");
    		client.sendSync(validationExchange);
    		client.done(validationExchange);
    		NormalizedMessage outMessage = validationExchange.getOutMessage();
    		Fault fault = validationExchange.getFault();
			if(outMessage != null) {
				sendSuccessMessage(client, outMessage.getContent());
			} else {
				sendErrorMessage(client, fault.getContent());
			}
    		
    	} else if(exchange.getStatus() == ExchangeStatus.ERROR) {
    		System.out.println("ErrorHandlerComponent: STATUS IS SUCCESS!!!!!!!!!!!");
    		return;
    		
    	} else if(exchange.getStatus() == ExchangeStatus.ERROR) {
    		System.out.println("ErrorHandlerComponent: STATUS IS ERROR!!!!!!!!!!!");
    		return;
    	}
    	exchange.setStatus(ExchangeStatus.DONE);
		channel.send(exchange);
	}
	
	private ServiceMixClient createClient() {
		try {
			ClientFactory factory = (ClientFactory) new InitialContext().lookup(ClientFactory.DEFAULT_JNDI_NAME);
			return factory.createClient();
		} catch(Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private void sendSuccessMessage(ServiceMixClient client, Source source) throws MessagingException {
		sendMessage(client, source,
				"service:http://esbinaction.com/order/validationSuccess");
	}
	  
	private void sendErrorMessage(ServiceMixClient client, Source source) throws MessagingException {
	    sendMessage(client, source,
	        	"service:http://esbinaction.com/order/validationError");
	}
	  
	private void sendMessage(ServiceMixClient client, Source source, String destination) throws MessagingException {
	    Destination returnDestination = 
	        client.createDestination(destination);
	    InOnly inOnlyExchange = returnDestination.createInOnlyExchange();
	    NormalizedMessage inMessage = inOnlyExchange.getInMessage();
	    inMessage.setContent(source);
	    client.send(inOnlyExchange);
	}

}
